
$(function () {

    /*------------定义全局变量-------------------------*/
    var deleteId = '';//删除的教师id
    var curPageNum = 1;//存放当前分页的页数


    /*-----------------初始化页面-------------------------------*/
    $("#top").load("top.html");
    $("#left").load('department_left.html',function () {
        menuActive(1,1)
    });

    //初始化教师列表
    queryTeacher({pageNum:1});

    //初始化学院下拉列表 -- 搜索
    requests.getReq(urls.getCollege,'',function (data) {
        if(data.status == 1){
            var collegeOpts = '';
            var list = data.data;
            for(var key in list){
                collegeOpts += '<option value="'+key+'">'+list[key]+'</option>>';
            }
            $("#collegeSel").append(collegeOpts);

        }else{
            showTip('服务器异常');
        }
    },function () {
        showTip('服务器异常');
    })

    //初始化学历下拉列表
    var degreeOpts = '';
    for(var key in commonEducation){
        degreeOpts += '<option value="'+key+'">'+commonEducation[key]+'</option>>';
    }
    $("#edit_edu_selc").append(degreeOpts);

    //初始化职称下拉列表
    var professOpts = '';
    for(var key in commonTitle){
        professOpts += '<option value="'+key+'">'+commonTitle[key]+'</option>>';
    }
    $("#edit_title_selc").append(professOpts);

    /*---------------事件绑定-------------------------------------------------------*/
    //删除信息弹框
    $("#teaList").on('click','.delete_tea',function () {
        $(".tip").fadeIn(100);
        deleteId = $(this).parents('tr').attr('data-userid');
    });
    $(".tiptop a").click(function () {
        $(".tip").fadeOut(200);
    });
    $(".cancel").click(function () {
        $(".tip").fadeOut(100);
    });
    $(".sure").click(function () {
        $(".tip").fadeOut(100);
        requests.getReq(urls.deleteTeacher,{userId:deleteId},
            function (data) {
                if(data.status == 1){
                    showTip('教师删除成功',function () {
                        queryTeacher({pageNum:curPageNum});
                    })
                }else{
                    showTip(data.message);
                }
            }
        )
    })

    //修改教师信息弹框
    $("#teaList").on('click','.edit_tea',function () {
        var item = $(this).parents('tr');
        $("#edit_userid").html(item.attr('data-userid'));
        $("#edit_name").html(item.attr('data-name'));
        $("#edit_edu_selc").val(item.attr('data-edu'));
        $("#edit_title_selc").val(item.attr('data-title'));
        $("#edit_level_input").val(item.attr('data-level'));
        $("#editBox").fadeIn(200);
    })
    $("#updateInfo").click(function () {
        var updateParam = {
            'userId': $("#edit_userid").html(),
            'teacherPostLevel': $("#edit_level_input").val(),
            'title': $("#edit_title_selc").val(),
            'education': $("#edit_edu_selc").val()
        }
        for(var key in updateParam){
            if(! $.trim(updateParam[key])){
                showTip('请完善信息后再提交');
                return;
            }
        }

        $("#editBox").fadeOut(200);
        requests.postReq(urls.editTeacherInfo,JSON.stringify(updateParam),
            function(data){
                if(data.status == 1){
                    showTip('教师'+$("#edit_name").html()+'信息修改成功',function () {
                        window.location.reload();
                    })
                }else{
                    showTip(data.message);
                }
            },
            function () {
                showTip('服务器错误');
            }
        )

    });

    //点击查询
    $("#searchTea").click(function () {
        var searchParam = {
            "pageNum": 1,
        }
        queryTeacher(searchParam);
    })

    //点击上一页
    $("#prevPage").click(function () {
        if(!$(this).hasClass('unClick')){
            queryTeacher({pageNum:curPageNum-1});
        }
    })
    //点击下一页
    $("#nextPage").click(function () {
        if(!$(this).hasClass('unClick')){
            queryTeacher({pageNum:curPageNum+1});
        }
    })


    /*---------------函数定义---------------------------------------------------*/
    //查询教师列表
    function queryTeacher(param) {
        param.pageSize = 10;
        param.collegeId = $("#collegeSel").val();
        param.userName = $("#teaName").val();
        requests.postReq(urls.teachList,JSON.stringify(param),initList,postError);
    }
    //查询接口返回成功
    function initList(data) {
        if(data.status == 1){
            data = data.data;
            var items = data.list;
            var teaListHtml = '';
            $.each(items,function (i,val) {
                teaListHtml += '<tr data-userid="'+val.userId+'" data-name="'+val.userName+'" data-title="'+val.title+'" data-edu="'+val.education+'" data-level="'+val.teacherPostLevel+'" >' +
                    '<td>'+val.userId+'</td>'+
                    '<td>'+val.userName+'</td>'+
                    '<td>'+commonTitle[val.title]+'</td>'+
                    '<td>'+val.teacherPostLevel+'</td>'+
                    '<td>'+commonEducation[val.education]+'</td>'+
                    '<td>'+compTime(val.hiringTime)+'</td>'+
                    '<td>'+val.collegeName+'</td>'+
                    '<td>'+val.researchName+'</td>'+
                    '<td><span class="edit_tea">编辑</span><span class="delete_tea">删除</span></td>'+
                    '</tr>';
            })
            $("#teaList").html(teaListHtml);

            $("#pagination .page_info").html('共<span>'+data.total+'</span>条记录，当前第<span>'+data.pageNum+'</span>页');
            pageBtnState(data.pageNum,data.hasNextPage,data.hasPreviousPage);
        }else{
            showTip('未查询到教师列表');
        }
    }
    //查询接口报错
    function postError() {
        showTip('服务器错误');
    }


})